layui.use(["layer",'jquery','laydate','table','form'],function () {
    var
        form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        table = layui.table;
    $("#advancedSearching").click(function () {
        layer.open({
            type:2,
            title:"高级检索",
            area:["800px","600px"],
            btn:["确定","取消"],
            content:"coupon-list-retrieval.html",
            yes:function(index,layero){
                var obj = layero.find("iframe").contents();
                var  couponName = obj.find("select[name=couponName]").val();
                var  grant = obj.find("select[name=grant]").val();
                var  startDate = obj.find("select[name=startDate]").val();
                var  endDate = obj.find("select[name=endDate]").val();
                var  online = obj.find("select[name=online]").val();

                var param={
                    'couponName':couponName,
                    'grant':grant,
                    'startDate':startDate,
                    'endDate':endDate,
                    'online':online
                }
                $.ajax({
                    url:"/JsUserCoupon/selectRetrieval",
                    data:param,
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        layer.close(index);
                        tableRender(data);
                    }
                });
            },
            btn2:function(layero,index){

            }

        });
    });
    //返回
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    //刷新
    $("#Refresh").click(function () {
        location.reload();
    });
    var param = {
        'couponName':"",
        'grant':"",
        'online':""
    }
    selectAllUserCoupon(param);
    $("#search").click(function () {
        var data = form.val('layForm');
        var couponName = data.couponName;
        var grant = data.grant;
        var online = data.online;
        var param = {
            'couponName':couponName,
            'grant':grant,
            'online':online
        }
        selectAllUserCoupon(param);
    });
    var diao = function diao(){
        selectAllUserCoupon(param);
    }
    setInterval(diao,300000);
    function selectAllUserCoupon(param){
        $.ajax({
            url:"/JsUserCoupon/getAllJsUserCoupon",
            data:param,
            type:'post',
            dataType:'json',
            success:function (data) {
                console.log(data);
                tableRender(data);

                form.on("switch(switchTest)",function (data) {

                    var couponId = data.elem.value;
                    var userId = JSON.parse(localStorage.getItem('user')).data.userId;
                    var online = 0;
                    var updateDate = "";
                    getTime();
                    if (data.elem.checked == true) {
                        online = 2;
                    }else{
                        online = 3;
                    }
                    //现在时间
                    function getTime(){
                        var date=new Date();
                        var year=date.getFullYear();
                        var month=date.getMonth()+1;
                        var day=date.getDate();
                        var hour=date.getHours();
                        var minute=date.getMinutes();
                        var second=date.getSeconds();
                        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
                        updateDate = dateStr;
                    }
                    function zeroFilling(data){
                        if(data<10){
                            data="0"+data;
                        }
                        return data;
                    }
                    var param = {
                        'couponId':couponId,
                        'online':online,
                        'updateId':userId,
                        'updateDate':updateDate
                    };
                    if(data.elem.checked == true){
                        $.ajax({
                            url:"/JsUserCoupon/updateByIdOnline",
                            data:param,
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                                if (data.data == true){
                                    layer.msg("修改成功");
                                } else{
                                    layer.msg("修改失败");
                                }
                            }
                        });
                    }else{
                        $.ajax({
                            url:"/JsUserCoupon/updateByIdOnline",
                            data:param,
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                                if (data.data == true){
                                    layer.msg("修改成功");
                                } else{
                                    layer.msg("修改失败");
                                }
                            }
                        });
                    }
                })
                table.on("tool(couponTable)",function (obj) {
                    var event = obj.event;
                    if(event == "toolSelect"){
                        layer.open({
                            type:2,
                            title:"优惠券详情",
                            area:["95%","99%"],
                            offset:"auto",
                            btn:["确定","取消"],
                            content:"coupon-list-select.html",
                            success:function (layero, index) {
                                var sonObj = layero.find('iframe').contents();
                                sonObj.find("th[name=couponCode]").text(obj.data.couponCode);
                                sonObj.find("th[name=couponName]").text(obj.data.couponName);
                                sonObj.find("th[name=startDate]").text(obj.data.startDate);
                                sonObj.find("th[name=endDate]").text(obj.data.endDate);
                                sonObj.find("th[name=online]").text(obj.data.online);
                                if (obj.data.online == 2) {
                                    sonObj.find("th[name=online]").text("上线");
                                }else if(obj.data.online == 3){
                                    sonObj.find("th[name=online]").text("下线");
                                }

                                if (obj.data.grant == 2){
                                    sonObj.find("th[name=grant]").text("自动发放");
                                } else if(obj.data.grant == 3){
                                    sonObj.find("th[name=grant]").text("活动领取");
                                }else if(obj.data.grant == 4){
                                    sonObj.find("th[name=grant]").text("人工发放");
                                }else if(obj.data.grant == 5){
                                    sonObj.find("th[name=grant]").text("无限制");
                                }
                                sonObj.find("th[name=issueQuantity]").text(obj.data.issueQuantity);
                                sonObj.find("th[name=dataDictValue]").text(obj.data.dataDictValue);
                            }
                        })
                    }else{
                        layer.confirm('确定删除当前优惠券吗?', {icon: 3, title:'删除提示'}, function(index){
                            $.ajax({
                                url:"/JsUserCoupon/deleteByIdJsUserCoupon",
                                data:{"couponId":obj.data.couponId},
                                type:'post',
                                dataType:'json',
                                success:function (data) {
                                    layer.msg(data);
                                }
                            })
                            layer.close(index);
                        });

                    }
                })
            }

        })
    }

    function tableRender(data){
        table.render({
            elem:"#couponTable",
            data:data.data,
            toolbar:"#toolbarFunction",
            cols:[[
                {type:'checkbox'},
                {field:"couponName",title:"优惠券名称",align:"center"},
                {field:"startDate",title:"开始时间",align:"center"},
                {field:"endDate",title:"结束时间",align:"center",templet:function(data){
                        if (data.endDate < getTime()){
                            $.ajax({
                                url:"/JsUserCoupon/updateJsUserCouponUsedUsedState",
                                data:{"couponId":data.couponId},
                                type:'post',
                                dataType:'json',
                                success:function (data) {
                                }
                            });
                        }
                        return data.endDate;
                    }},
                {field:"online",title:"不上线/上线",align:"center",templet:function (data) {
                        if(data.online=='2'){
                            return '<input type=\"checkbox\" value="'+data.couponId+'" lay-skin=\"switch\" checked lay-filter="switchTest">';
                        }else if(data.online=='3'){
                            return '<input type=\"checkbox\" value="'+data.couponId+'" lay-skin=\"switch\" lay-filter="switchTest"\>';
                        }
                    }},
                {field:"grant",title:"发放方式",align:"center",templet:function (data) {
                        if (data.grant == 2){
                            return "自动发放";
                        } else if (data.grant == 3) {
                            return "活动领取";
                        }else if (data.grant == 4) {
                            return "人工发放";
                        }else{
                            return "无限制";
                        }
                    }},
                {field:"issueQuantity",title:"发行数",align:"center"},
                {field:"",title:"剩余数",align:"center",templet:function (data) {
                    if(parseFloat(data.used) == 0){
                        return parseFloat(data.issueQuantity);
                    }else{
                        return parseFloat(data.issueQuantity) - parseFloat(data.used);
                    }

                    }},

                {field:"",title:"操作",align:"center",toolbar:"#toolFunction"}
            ]],
            page:true
        })
    }
    //现在时间
    function getTime(){
        var date=new Date();
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        return dateStr;
    }
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }
    addCoupon = function addCoupon() {
        window.location.href="/coupon/list/coupon-list-addCoupon.html";
    }
})